package vn.edu.ptithcm.pim.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.LockMode;
import org.hibernate.Transaction;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import vn.edu.ptithcm.pim.dom.Task;
import vn.edu.ptithcm.pim.dom.TaskSeverity;

@SuppressWarnings({ "rawtypes" })
public class TaskSeverityDao extends HibernateDaoSupport {

	public void save(TaskSeverity taskSeverity) {
		try {
			Transaction tx = getSession().beginTransaction();
			getHibernateTemplate().save(taskSeverity);
			tx.commit();
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public void saveOrUpdate(TaskSeverity taskSeverity) {
		try {
			Transaction tx = getSession().beginTransaction();
			getHibernateTemplate().saveOrUpdate(taskSeverity);
			tx.commit();
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public void lock(TaskSeverity taskSeverity) {
		try {
			Transaction tx = getSession().beginTransaction();
			getHibernateTemplate().lock(taskSeverity, LockMode.NONE);
			tx.commit();
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public void delete(TaskSeverity taskSeverity) {
		try {
			Transaction tx = getSession().beginTransaction();
			getHibernateTemplate().delete(taskSeverity);
			tx.commit();
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public TaskSeverity merge(TaskSeverity taskSeverity) {
		try {
			return (TaskSeverity) getHibernateTemplate().merge(taskSeverity);
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public TaskSeverity findById(java.lang.Long id) {
		try {
			return (TaskSeverity) getHibernateTemplate().get(
					TaskSeverity.class, id);
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public List findByExample(TaskSeverity taskSeverity) {
		try {
			return getHibernateTemplate().findByExample("TaskSeverity",
					taskSeverity);
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public List getAll() {
		try {
			return getHibernateTemplate().loadAll(TaskSeverity.class);
		} catch (RuntimeException re) {
			throw re;
		}
	}

	@SuppressWarnings("unchecked")
	public List<TaskSeverity> getSeveritiesToReport(Long projectId) {
		Criteria c = getSession().createCriteria(Task.class);
		c.setProjection(Projections.property("taskSeverity"));
		c.createAlias("project", "project");
		c.add(Restrictions.eq("project.projectid", projectId));
		c.uniqueResult();
		return c.list();
	}
}
